package com.hyg.lib_music;

import android.bluetooth.BluetoothA2dp;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.hyg.lib_base.mainUtils.BluetoothUtils;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.constant.Constants;
import com.hyg.lib_common.ui.activity.BlueToothListActivity;

import java.lang.reflect.Method;

//@Route(path = Constants.PATH_ACTIVITY_MODULE_HEALTH_MUSICBED)
public class MusicMainStartActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_test);
        init();
    }

    public void init() {

    }

    public void test1111(View view) {
        BluetoothUtils utils = new BluetoothUtils(this);
        if (!utils.getBluetoothEnable()) {
            Toast.makeText(this, "蓝牙不可用", Toast.LENGTH_SHORT).show();
        } else if (!utils.getBluetoothIsOpen()) {
            Toast.makeText(this, "请打开蓝牙", Toast.LENGTH_SHORT).show();

            ToIntent(Constants.PATH_ACTIVITY_MODULE_HEALTH_MUSICBED, false);
            overridePendingTransition(com.hyg.lib_common.R.anim.fade, com.hyg.lib_common.R.anim.hold);
        }
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(this, new BluetoothProfile.ServiceListener() {
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                if (proxy instanceof BluetoothA2dp) {
                    BluetoothA2dp mBluetoothA2dp = (BluetoothA2dp) proxy;
                    try {
                        Class<?> bluetoothA2dp = Class.forName("android.bluetooth.BluetoothA2dp");
                        Method getActiveDevice = bluetoothA2dp.getMethod("getActiveDevice");
                        BluetoothDevice activeDevice = (BluetoothDevice) getActiveDevice.invoke(mBluetoothA2dp);
                        if (activeDevice != null && activeDevice.getName().contains("Audio")) {
                            Log.i("lgb", "onServiceConnected:0000 ");
                            ToIntent(Constants.PATH_ACTIVITY_MODULE_HEALTH_MUSICBED, false);
                            overridePendingTransition(com.hyg.lib_common.R.anim.fade, com.hyg.lib_common.R.anim.hold);
                        } else {
                            Log.i("lgb", "onServiceConnected:111111 ");
                            Intent intent = new Intent(MusicMainStartActivity.this, BlueToothListActivity.class);
                            intent.putExtra("type", "1");
                            startActivity(intent);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.i("lgb", "onServiceConnected2222: Exception");
                    }
                }
            }

            @Override
            public void onServiceDisconnected(int profile) {
            }
        }, BluetoothProfile.A2DP);
    }


}